package com.actionpals.components.controls.calendar
{
	import com.actionpals.components.controls.Calendar;
	import com.actionpals.graphics.Gradient;
	import com.actionpals.graphics.RectangleShape;
	
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class CalendarDayNameHeader extends Sprite
	{
		private var calendar:Calendar;
		private var bg:RectangleShape;
		private var border:RectangleShape;
		private var dayNames:Array = new Array();
		
		public function CalendarDayNameHeader( calendar:Calendar )
		{
			super();
			
			this.calendar = calendar;
			
			init();
		}
		
		public function drawNow():void
		{
			draw();
		}
		
		protected function init():void
		{
			create();
			draw();
		}
		
		protected function create():void
		{
			createBG();
			createBorder();
		}
		
		protected function createBG():void
		{
			bg = new RectangleShape( 100, 20 );
			addChild( bg );
		}
		
		protected function createBorder():void
		{
			border = new RectangleShape( 100, 1 );
			addChild( border );
		}
		
		protected function draw():void
		{
			bg.width = calendar.width - calendar.style.borderThickness * 2;
			bg.height = ( calendar.style.dayNameHeaderStyle.percentHeight / 100 ) * calendar.height;
			var matrix:Matrix = new Matrix();
			var degrees:Number = 90;
			var radians:Number = ( degrees / 180 ) * Math.PI;
			matrix.createGradientBox( bg.width, bg.height, radians );
			bg.fillGradient = new Gradient( GradientType.LINEAR, calendar.style.dayNameHeaderStyle.colors, calendar.style.dayNameHeaderStyle.alphas, [ 0, 255 ], matrix );
			bg.x = bg.y = calendar.style.borderThickness;
			bg.drawNow();
			
			border.width = calendar.width - calendar.style.borderThickness * 2;
			border.height = calendar.style.dayNameHeaderStyle.borderThickness;
			border.fillColor = calendar.style.dayNameHeaderStyle.borderColor;
			border.fillAlpha = calendar.style.dayNameHeaderStyle.borderAlpha;
			border.x = calendar.style.borderThickness;
			border.y = bg.height - border.height;
			border.drawNow();
			
			while( dayNames.length ) removeChild( dayNames.shift() );
			
			for( var i:int = 0; i<calendar.dayNames.length; i++ )
			{
				var dayName:Sprite = new Sprite();
				dayName.x = calendar.width / calendar.dayNames.length*i;
				addChild( dayName );
				dayNames.push( dayName );
				
				var dayNameBG:RectangleShape = new RectangleShape( calendar.width/calendar.dayNames.length, ( calendar.style.dayNameHeaderStyle.percentHeight / 100 ) * calendar.height );
				dayNameBG.alpha = 0;
				dayName.addChild( dayNameBG );
				
				var dayField:TextField = new TextField();
				dayField.text = calendar.dayNames[ i ];
				dayField.autoSize = TextFieldAutoSize.CENTER;
				dayField.embedFonts = calendar.style.dayNameHeaderStyle.embedFonts;
				dayField.selectable = false;
				dayField.setTextFormat( calendar.style.dayNameHeaderStyle.textFormat );
				dayField.x = dayName.width/2 - dayField.width/2;
				dayField.y = dayName.height/2 - dayField.textHeight/2 - border.height/2;
				
				dayName.addChild( dayField );
			}
		}
		
	}
}